package utils

import (
	"context"
	"encoding/json"
	"fmt"
)

type AuthInfo struct {
	AppId string  `json:"app_id"`
	TimeStamp int64 `json:"time_stamp"`
}

func Authorize(ctx context.Context, sign string, authInfo *AuthInfo, appKey string) (bool, error) {
	//if ykenv.IsDevMode() == false {
		str := fmt.Sprintf("%s&%d&%s", authInfo.AppId, authInfo.TimeStamp, appKey)
		newSign, err := HmacSha256(str, appKey)
		if err != nil {
			return false, err
		}

		if sign != newSign {
			return false, nil
		}
	//}

	return true, nil
}

func TestTool()  {
	var authInfo AuthInfo
	authInfo.AppId = "micro-gfyx-user-service"
	authInfo.TimeStamp = 123456789
	str := fmt.Sprintf("%s&%d&%s", authInfo.AppId, authInfo.TimeStamp, "3F9Espv8lrcZ2iDA4MqJhTy6QXUzoYax")
	sign, err := HmacSha256(str, "3F9Espv8lrcZ2iDA4MqJhTy6QXUzoYax")
	if err != nil {
		//fmt.Println("sign err: %v", sign)
		return
	}
	a, _ := json.Marshal(authInfo)
	fmt.Println("str: ", str, "========bearer: ", fmt.Sprintf("%s.%s", sign, string(a)))
}
